<?php
	include("initialize.php");
	include("write_functions.php");
	connect("localhost", "root", "", "gradegriddatabase");
?>
<html>
	<head>
		<title>GradeGrid - Edit Course</title>
		<style type="text/css">
			p, .pMirror{
				font-family: "Trebuchet MS", sans-serif;
				font-size: 13px;
				text-align: justify;
				color: #000000;
				margin-right: 8px;
			}
		
			h1{
				font-family: Arial, Helvetica, sans-serif;
				font-size: 18px;
				color: #000000;
			}
			
			.form_section{
				background-color: #DADADA;
				-moz-border-radius: 5px;
				-webkit-border-radius: 5px;
				border-radius: 5px;
				padding: 0px 4px;
				width: 100%;
				min-width: 30%;
			}
		</style>
		<script type="text/javascript" language="javascript">
			function createCourse(){
				alert("Course edited.")
				document.getElementById("courseform").submit()
			}
		</script>
		<?php
			$courseid = $_GET["courseid"];
			$course_query = mysql_query("SELECT * FROM course where course_id = '$courseid'");
			$course_array = mysql_fetch_array($course_query);
			$coursename = $course_array["course_name"];
			$courseschoolid = $course_array["school_id"];
			
			if(isset($_POST["coursename"])){
				$coursename = mysql_real_escape_string($_POST["coursename"]);
				//$coursedescription = mysql_real_escape_string($_POST["coursedescription"]);
				$schoolid = mysql_real_escape_string($_POST["school"]);
				
				modifycourse($courseid, $coursename, $schoolid);
				
				mysql_query("UPDATE course SET schemeid = '" . $_POST["schemeid"] . "' WHERE course_id='$courseid'");
			}
			
		?>
	</head>
	<body>
		<div class="form_section"><h1>Open a new course</h1></div>
		<p>All fields in this form are required.</p>
		<form action="editCourse.php?courseid=<?php echo $courseid; ?>" method="post" id="courseform">
		<p>Course name:<br />
		<input type="text" name="coursename" value="<?php echo $coursename; ?>"/></p>
		
		<!--<p>Course description:<br />
		<input type="text" name="coursedescription" /></p>-->
		
		<p>In school:<br />
		<select name="school">
		<?php
			$username = mysql_real_escape_string($_SESSION["username"]);
			$accountid_query = mysql_query("SELECT account_id FROM account WHERE username = '$username'");
			$accountid_array = mysql_fetch_array($accountid_query);
			$accountid = mysql_real_escape_string($accountid_array["account_id"]);
			
			$schools_query = mysql_query("SELECT school_name, school_id FROM school WHERE account_id = '$accountid'");
			$schools_limit = mysql_num_rows($schools_query);
			$i = 0;
			
			if($courseschoolid == 0){
				echo "<option value='$schoolid' selected='selected'></option>";
			}

			while($i < $schools_limit){
				mysql_data_seek($schools_query, $i);
				$schools_array = mysql_fetch_array($schools_query);
				$schoolname = $schools_array["school_name"];
				$schoolid = $schools_array["school_id"];
				
				if($schoolid == $courseschoolid){
					echo "<option value='$schoolid' selected='selected'>$schoolname</option>\n";
				} else{
					echo "<option value='$schoolid'>$schoolname</option>\n";
				}
				$i++;
			}
		?>
		</select></p>
		
		<p>Use grade bracket scheme:<br />
		<select name="schemeid">
		<?php
			$scheme_query = mysql_query("SELECT scheme_name, scheme_id FROM scheme WHERE account_id = '$accountid'");
			$scheme_limit = mysql_num_rows($scheme_query);
			$scheme_index = 0;
			
			while($scheme_index < $scheme_limit){
				mysql_data_seek($scheme_query, $scheme_index);
				$scheme_tuple = mysql_fetch_array($scheme_query);
				echo "<option value='" . $scheme_tuple["scheme_id"] . "'>" . $scheme_tuple["scheme_name"] . "</option>\n";
				$scheme_index++;
			}
		?>
		</select></p>
		
		<input type="button" onclick="createCourse()" value="Edit Course" /> <input type="reset" value="Reset Form" />
		</form>
	</body>
</html>